#include <vector>
#include<unordered_map>
#include<iostream>
class Solution {
public:
    int subarraySum(std::vector<int>& nums, int k) {
        std::unordered_map<int, int> map;
        map.insert({0, 1});
        int N = nums.size();
        int sum = 0;
        
        int ans = 0;
        if(k==0) ans++;
        for (int i = 0; i < N; ++i) {
            sum += nums[i];

            if (map.find(sum - k) != map.end()) {
                ans+=map[sum - k];
            }
            if (map.find(sum) == map.end()) {
                map.insert({sum, 1});
            }else
            {
                map[sum] ++;
            }
        }
        if (k==0) return ans-1;
        return ans;
    }
};